package com.ruoyi.orders.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.orders.mapper.OrdersMapper;
import com.ruoyi.orders.domain.Orders;
import com.ruoyi.orders.service.IOrdersService;

/**
 * 理疗订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-28
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Orders> selectOrdersAll() {
        return ordersMapper.selectOrdersAll();
    }

    /**
     * 查询理疗订单
     * 
     * @param id 理疗订单主键
     * @return 理疗订单
     */
    @Override
    public Orders selectOrdersById(Long id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询理疗订单列表
     * 
     * @param orders 理疗订单
     * @return 理疗订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        // 判断角色
        LoginUser loginUser = SecurityUtils.getLoginUser();
        List<SysRole> roles = loginUser.getUser().getRoles();
        Long roleId = roles.get(0).getRoleId();
        Long userId = loginUser.getUserId();
        // 病人，只能查自己的订单信息
        if(roleId.equals(100L)){
            orders.setUserId(userId);
        }
        return ordersMapper.selectOrdersList(orders);
    }

    /**
     * 新增理疗订单
     * 
     * @param orders 理疗订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders)
    {
        orders.setCreateTime(DateUtils.getNowDate());
        orders.setOrderTime(DateUtils.getNowDate());
        orders.setNumber(UUID.fastUUID().toString());
        Long userId = SecurityUtils.getLoginUser().getUserId();
        orders.setUserId(userId);
        return ordersMapper.insertOrders(orders);
    }

    /**
     * 修改理疗订单
     * 
     * @param orders 理疗订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        orders.setUpdateTime(DateUtils.getNowDate());
        Long status = orders.getStatus();
        if(status.equals(11L)) { //状态要修改为已支付，则设置支付时间
            orders.setCheckoutTime(DateUtils.getNowDate());
        }
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除理疗订单
     * 
     * @param ids 需要删除的理疗订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Long[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除理疗订单信息
     * 
     * @param id 理疗订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Long id)
    {
        return ordersMapper.deleteOrdersById(id);
    }
}
